// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const voiceCollection = db.collection('voice')
const managertoolCollection = db.collection('manager_tool')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const dataSize = event.dataSize || 5

  let swiperList = []
  let swiperList1 = []
  let swiperList2 = []

  await managertoolCollection.doc('d7cac9a4-2649-4d3b-8fcf-7eaae2902dcd').get().then(res => {
    if(res.data.recommendState){
      swiperList1.push({
        id: '',
        type: 'image',
        url: res.data.recommendImg1 == '' ? 'cloud://conch-voice-test-8c0757.636f-conch-voice-test-8c0757/bg.jpeg' : res.data.recommendImg1,
        abstract: res.data.recommendText1,
        likeCount: ''
      })
      swiperList1.push({
        id: '',
        type: 'image',
        url: res.data.recommendImg2 == '' ? 'cloud://conch-voice-test-8c0757.636f-conch-voice-test-8c0757/bg.jpeg' : res.data.recommendImg2,
        abstract: res.data.recommendText2,
        likeCount: ''
      })
    }
  })

  await voiceCollection.limit(dataSize).orderBy('likeCount', 'desc').get().then(res => {
    swiperList2 = res.data.map(item => {
      return {
        id: item._id,
        type: 'image',
        url: item.coverImg == '' ? 'cloud://conch-voice-test-8c0757.636f-conch-voice-test-8c0757/bg.jpeg' : item.coverImg,
        abstract: item.voiceContent.replace(/<\/?.+?>/g, "").substring(0, 15) + '...',
        likeCount: item.likeCount
      }
    })
  })
  swiperList = swiperList1.concat(swiperList2)
  return swiperList
}